﻿using System.Diagnostics;

namespace TianYu.Core.Common
{
    [DebuggerStepThrough]
    public static class Check
    {
        /// <summary>确保参数不为null</summary>
        /// <typeparam name="T">参数值类型</typeparam>
        /// <param name="value">参数值</param>
        /// <param name="parameterName">导致异常的参数的名称。</param>
        /// <exception cref="TYArgumentNullException"/>
        /// <returns>参数值</returns>
        public static T NotNull<T>(T value, string parameterName)
        {
            if (value is null)
            {
                NotEmpty(parameterName, nameof(parameterName));
                throw new TYArgumentNullException(parameterName);
            }
            return value;
        }

        /// <summary>确保字符串不为空白字符</summary>
        /// <typeparam name="T">参数值类型</typeparam>
        /// <param name="value">字符串</param>
        /// <param name="parameterName">导致异常的参数的名称。</param>
        /// <exception cref="TYArgumentNullException"/>
        /// <exception cref="TYBaseException"/>
        /// <returns>字符串</returns>
        public static string NotEmpty(string value, string parameterName)
        {
            Exception e = null;
            if (value is null)
            {
                e = new TYArgumentNullException(parameterName);
            }
            else if (value.Trim().Length == 0)
            {
                e = new TYBaseException("1001", parameterName);
            }
            if (e != null)
            {
                NotEmpty(parameterName, nameof(parameterName));
                throw e;
            }
            return value;
        }

        /// <summary>确保枚举不为null和空集合</summary>
        /// <typeparam name="T">参数值类型</typeparam>
        /// <param name="value">枚举值</param>
        /// <param name="parameterName">导致异常的参数的名称。</param>
        /// <exception cref="TYArgumentNullException"/>
        /// <exception cref="TYBaseException"/>
        /// <returns>枚举值</returns>
        public static IEnumerable<T> NotEmpty<T>(IEnumerable<T> value, string parameterName)
        {
            if (!value.Any())
            {
                NotEmpty(parameterName, nameof(parameterName));
                throw new TYArgumentNullException(parameterName);
            }
            return value;
        }

        /// <summary>确保枚举不为null</summary>
        /// <typeparam name="T">枚举值类型</typeparam>
        /// <param name="value">枚举值</param>
        /// <param name="parameterName">导致异常的参数的名称。</param>
        /// <exception cref="TYArgumentNullException"/>
        /// <exception cref="TYBaseException"/>
        /// <returns>枚举值</returns>
        public static IEnumerable<T> HasNoNull<T>(IEnumerable<T> value, string parameterName) where T : class
        {
            NotNull(value, parameterName);
            if (value.Any(e => e == null))
            {
                NotEmpty(parameterName, nameof(parameterName));
                throw new TYBaseException("1001", parameterName);
            }
            return value;
        }

        /// <summary>确保字符串类型的枚举不包含空白字符串</summary>
        /// <typeparam name="T">枚举值类型</typeparam>
        /// <param name="value">字符串类型的枚举值</param>
        /// <param name="parameterName">导致异常的参数的名称。</param>
        /// <exception cref="TYArgumentNullException"/>
        /// <exception cref="TYBaseException"/>
        /// <returns>字符串类型的枚举值</returns>
        public static IEnumerable<string> HasNoEmpty(IEnumerable<string> value, string parameterName)
        {
            NotNull(value, parameterName);
            if (value.Any(s => string.IsNullOrWhiteSpace(s)))
            {
                NotEmpty(parameterName, nameof(parameterName));
                throw new TYBaseException("1001", parameterName);
            }
            return value;
        }
    }
}
